--[=[
 象棋规则
--local tb = {
	--	c0 = "车",
	--	m0 = "马",
	--	x0 = "相",
	--	s0 = "仕",
	--	p0 = "炮",
	--	j0 = "帅",
	--	c1 = "车",
	--	m1 = "马",
	--	x1 = "相",
	--	s1 = "仕",
	--	p1 = "炮",
	--	z0 = "兵",
	--	z1 = "兵",
	--	z2 = "兵",
	--	z3 = "兵",
	--	z4 = "兵",
	--
	--	C0 = "车",
	--	M0 = "马",
	--	X0 = "象",
	--	S0 = "仕",
	--	P0 = "炮",
	--	J0 = "将",
	--	C1 = "车",
	--	M1 = "马",
	--	X1 = "象",
	--	S1 = "仕",
	--	P1 = "炮",
	--	Z0 = "卒",
	--	Z1 = "卒",
	--	Z2 = "卒",
	--	Z3 = "卒",
	--	Z4 = "卒",
	--}

]=]


local random = math.random
local tableCopy = table.copy

local M = class("Rule")

function  M:ctor()
	self.map = {
		{'C0','M0','X0','S0','J0','S1','X1','M1','C1'},
		{'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0'},
		{'0' ,'P0','0' ,'0' ,'0' ,'0' ,'0' ,'P1','0' },
		{'Z0','0' ,'Z1','0' ,'Z2','0' ,'Z3','0' ,'Z4'},
		{'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0'},
		{'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0'},
		{'z0','0' ,'z1','0' ,'z2','0' ,'z3','0' ,'z4'},
		{'0' ,'p0','0' ,'0' ,'0' ,'0' ,'0' ,'p1','0' },
		{'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' },
		{'c0','m0','x0','s0','j0','s1','x1','m1','c1'}
	};

	self.mans = {}
end


--把坐标生成着法
function M:createMove(map,x,y,newX,newY)
	print("craeteMove:", x,y,newX,newY)
	local h = "";
	local man = self.mans[map[y][x]];
	h = h .. man.text;
	map[newY][newX] = map[y][x];
	map[y][x] = "0"
	if (man.my==1) then
		local mumTo = {"一","二","三","四","五","六","七","八","九","十"};
		newX = 10-newX;
		h = h .. mumTo[10-x];
		if (newY > y) then
			h = h .. "退";
			if (man.pater == "m" or man.pater == "s" or man.pater == "x") then
				h = h .. mumTo[newX];
			else
				h = h .. mumTo[newY - y];
			end
		elseif (newY < y) then
			h = h .."进";
			if (man.pater == "m" or man.pater == "s" or man.pater == "x") then
				h = h .. mumTo[newX];
			else
				h = h .. mumTo[y - newY];
			end
		else
			h = h .."平";
			h = h ..mumTo[newX];
		end
	else
		local mumTo= {"１","２","３","４","５","６","７","８","９","10"};
		h = h .. mumTo[x];
		if (newY > y) then
			h = h .. "进";
			if (man.pater == "M" or man.pater == "S" or man.pater == "X") then
				h = h .. mumTo[newX];
			else
				h = h .. mumTo[newY - y];
			end
		elseif (newY < y) then
			h = h .. "退";
			if (man.pater == "M" or man.pater == "S" or man.pater == "X") then
				h = h .. mumTo[newX];
			else
				h = h .. mumTo[y - newY];
			end
		else
			h = h .. "平";
			h = h .. mumTo[newX];
		end
	end
	return h;
end




--棋子能走的着点
--self.bylaw ={}
--车
function M:c(x,y,map,my)
	--print("@@@@@@@@@", x, y, map, my)
	local d={};
	local min = 1
	local max = 9
	--左侧检索
	for i=x-1, min, -1 do
		if (map[y][i] ~= "0") then
			if self.mans[map[y][i]].my~=my then
				table.insert(d, {i, y});
			end
			break
		else
			table.insert(d, {i, y});
		end
	end
	--右侧检索
	for  i=x+1, max do
		if (map[y][i] ~= "0") then
			if (self.mans[map[y][i]].my~=my) then
				table.insert(d, {i, y});
			end
			break
		else
			table.insert(d, {i, y});
		end
	end
	--上检索
	for  i = y-1, min, -1 do
		if map[i][x] ~= "0" then
			if (self.mans[map[i][x]].my~=my) then
				table.insert(d, {x, i})
			end
			break
		else
			table.insert(d, {x, i})
		end
	end
	--下检索
	for  i = y+1, 10 do
		if map[i][x] ~= "0" then
			if (self.mans[map[i][x]].my~=my) then
				table.insert(d, {x, i})
			end
			break
		else
			table.insert(d, {x, i})
		end
	end
	--print("d", tostring(d))
	return d;
end

--马
function M:m(x,y,map,my)
	local d={};
	--1点
	if ( y-2>= 1 and x+1<= 9 and "0" == self.map[y-1][x] and(not self.mans[map[y-2][x+1]] or self.mans[map[y-2][x+1]].my~=my)) then
		table.insert(d, {x+1,y-2})
	end
	--2点
	if ( y-1>= 1 and x+2<= 9 and "0" == self.map[y][x+1] and(not self.mans[map[y-1][x+2]] or self.mans[map[y-1][x+2]].my~=my)) then
		table.insert(d, {x+2,y-1})
	end
	--4点
	if ( y+1<= 9 and x+2<= 9 and "0" == self.map[y][x+1] and(not self.mans[map[y+1][x+2]] or self.mans[map[y+1][x+2]].my~=my)) then
		table.insert(d, {x+2,y+1})
	end
	--5点
	if ( y+2<= 9 and x+1<= 9 and "0" == self.map[y+1][x] and(not self.mans[map[y+2][x+1]] or self.mans[map[y+2][x+1]].my~=my)) then
		table.insert(d, {x+1,y+2})
	end
	--7点
	if ( y+2<= 9 and x-1>= 1 and "0" == self.map[y+1][x] and(not self.mans[map[y+2][x-1]] or self.mans[map[y+2][x-1]].my~=my)) then
		table.insert(d, {x-1,y+2})
	end
	--8点
	if ( y+1<= 9 and x-2>= 1 and "0" == self.map[y][x-1] and(not self.mans[map[y+1][x-2]] or self.mans[map[y+1][x-2]].my~=my)) then
		table.insert(d, {x-2,y+1})
	end
	--10点
	if ( y-1>= 1 and x-2>= 1 and "0" == self.map[y][x-1] and(not self.mans[map[y-1][x-2]] or self.mans[map[y-1][x-2]].my~=my)) then
		table.insert(d, {x-2,y-1})
	end
	--11点
	if ( y-2>= 1 and x-1>= 1 and "0" == self.map[y-1][x] and(not self.mans[map[y-2][x-1]] or self.mans[map[y-2][x-1]].my~=my)) then
		table.insert(d, {x-1,y-2})
	end

	return d;
end

--相
function M:x(x,y,map,my)
	local d={};
	if (my==1)then --红方
		--print("________66666666661", x, y ,self.map[y-1][x+1], self.mans[map[y-2][x-2]])
		--4点半
		if ( y+2<= 10 and x+2<= 9 and self.map[y+1][x+1] == "0" and (not self.mans[map[y+2][x+2]] or self.mans[map[y+2][x+2]].my~=my)) then
			table.insert(d, {x+2,y+2}) end
		--7点半
		if ( y+2<= 10 and x-2>= 1 and self.map[y+1][x-1] == "0" and (not self.mans[map[y+2][x-2]] or self.mans[map[y+2][x-2]].my~=my)) then
			table.insert(d, {x-2,y+2}) end
		--1点半
		if ( y-2>= 5 and x+2<= 9 and self.map[y-1][x+1] == "0" and (not self.mans[map[y-2][x+2]] or self.mans[map[y-2][x+2]].my~=my)) then
			table.insert(d, {x+2,y-2}) end
		--10点半
		if ( y-2>= 5 and x-2>= 1 and self.map[y-1][x-1] == "0" and (not self.mans[map[y-2][x-2]] or self.mans[map[y-2][x-2]].my~=my)) then
			table.insert(d, {x-2,y-2}) end
	else
		--4点半
		if ( y+2<= 4 and x+2<= 9 and self.map[y+1][x+1] == "0" and (not self.mans[map[y+2][x+2]] or self.mans[map[y+2][x+2]].my~=my)) then
			table.insert(d, {x+2,y+2}) end
		--7点半
		if ( y+2<= 4 and x-2>= 1 and self.map[y+1][x-1] == "0" and (not self.mans[map[y+2][x-2]] or self.mans[map[y+2][x-2]].my~=my)) then
			table.insert(d, {x-2,y+2}) end
		--1点半
		if ( y-2>= 1 and x+2<= 9 and self.map[y-1][x+1] == "0" and (not self.mans[map[y-2][x+2]] or self.mans[map[y-2][x+2]].my~=my)) then
			table.insert(d, {x+2,y-2}) end
		--10点半
		if ( y-2>= 1 and x-2>= 1 and self.map[y-1][x-1] == "0" and (not self.mans[map[y-2][x-2]] or self.mans[map[y-2][x-2]].my~=my)) then
			table.insert(d, {x-2,y-2}) end
	end
	return d;
end

--士
function M:s(x,y,map,my)
	local d={};
	if (my==1) then --红方
		--4点半
		if ( y+1<= 9 and x+1<= 5 and (not self.mans[map[y+1][x+1]] or self.mans[map[y+1][x+1]].my~=my)) then table.insert(d, {x+1,y+1}) end
		--7点半
		if ( y+1<= 9 and x-1>= 3 and (not self.mans[map[y+1][x-1]] or self.mans[map[y+1][x-1]].my~=my)) then table.insert(d, {x-1,y+1}) end
		--1点半
		if ( y-1>= 7 and x+1<= 5 and (not self.mans[map[y-1][x+1]] or self.mans[map[y-1][x+1]].my~=my)) then table.insert(d, {x+1,y-1}) end
		--10点半
		if ( y-1>= 7 and x-1>= 3 and (not self.mans[map[y-1][x-1]] or self.mans[map[y-1][x-1]].my~=my)) then table.insert(d, {x-1,y-1}) end
	else
		--4点半
		if ( y+1<= 2 and x+1<= 5 and (not self.mans[map[y+1][x+1]] or self.mans[map[y+1][x+1]].my~=my)) then table.insert(d, {x+1,y+1}) end
		--7点半
		if ( y+1<= 2 and x-1>= 3 and (not self.mans[map[y+1][x-1]] or self.mans[map[y+1][x-1]].my~=my)) then table.insert(d, {x-1,y+1}) end
		--1点半
		if ( y-1>= 1 and x+1<= 5 and (not self.mans[map[y-1][x+1]] or self.mans[map[y-1][x+1]].my~=my)) then table.insert(d, {x+1,y-1}) end
		--10点半
		if ( y-1>= 1 and x-1>= 3 and (not self.mans[map[y-1][x-1]] or self.mans[map[y-1][x-1]].my~=my)) then table.insert(d, {x-1,y-1}) end
	end
	return d;

end

--将
function M:j(x,y,map,my)
	local d={};
	local isNull = (function ()
		local y1=self.mans["j0"].y;
		local x1=self.mans["J0"].x;
		local y2=self.mans["J0"].y;
		for  i=y1-1, y2, -1 do
			if (map[i][x1]) then return false end
		end
		return true;
	end)();

	if (my==1) then --红方
		--下
		if ( y+1<= 9  and (not self.mans[map[y+1][x]] or self.mans[map[y+1][x]].my~=my)) then table.insert(d, {x,y+1}) end
		--上
		if ( y-1>= 8 and (not self.mans[map[y-1][x]] or self.mans[map[y-1][x]].my~=my)) then table.insert(d, {x,y-1}) end
		--老将对老将的情况
		if ( self.mans["j0"].x == self.mans["J0"].x and isNull) then table.insert(d, {self.mans["J0"].x,self.mans["J0"].y}) end

	else
		--下
		if ( y+1<= 2  and (not self.mans[map[y+1][x]] or self.mans[map[y+1][x]].my~=my)) then table.insert(d, {x,y+1}) end
		--上
		if ( y-1>= 1 and (not self.mans[map[y-1][x]] or self.mans[map[y-1][x]].my~=my)) then table.insert(d, {x,y-1}) end
		--老将对老将的情况
		if ( self.mans["j0"].x == self.mans["J0"].x and isNull) then table.insert(d, {self.mans["j0"].x,self.mans["j0"].y}) end
	end
	--右
	if ( x+1<= 6  and (not self.mans[map[y][x+1]] or self.mans[map[y][x+1]].my~=my)) then table.insert(d, {x+1,y}) end
	--左
	if ( x-1>= 4 and (not self.mans[map[y][x-1]] or self.mans[map[y][x-1]].my~=my)) then table.insert(d, {x-1,y}) end
	return d;
end

--炮
function M:p(x,y,map,my)
	local d={};
	--左侧检索
	local n=0;
	for  i=x-1, 1, -1 do
		if map[y][i] ~= "0" then
			if (n==0) then
				n = n + 1
			else
				if (self.mans[map[y][i]].my~=my) then table.insert(d, {i, y}) end
				break
			end
		else
			if(n==0) then table.insert(d, {i, y}) end
		end
	end
	--右侧检索
	local n=0;
	for  i=x+1, 9 do
		if map[y][i] ~= "0" then
			if (n==0) then
				n = n + 1
			else
				if (self.mans[map[y][i]].my~=my) then table.insert(d, {i, y}); end
				break
			end
		else
			if(n==0) then table.insert(d, {i, y}) end
		end
	end
	--上检索
	local n=0;
	for  i = y-1, 1, -1 do
		if map[i][x] ~= "0" then
			if (n==0) then
				n = n + 1
			else
				if (self.mans[map[i][x]].my ~= my) then
					table.insert(d, {x, i});
				end
				break
			end
		else
			if(n==0) then
				table.insert(d, {x, i})
			end
		end
	end
	--下检索
	local n=0;
	for  i = y+1, 10 do
		if map[i][x] ~= "0" then
			if (n==0) then
				n = n + 1
			else
				if (self.mans[map[i][x]].my ~= my) then
					table.insert(d, {x, i})
				end
				break
			end
		else
			if(n==0) then
				table.insert(d, {x, i})
			end
		end
	end
	return d;
end

--卒
function M:z(x,y,map,my)
	local d={};
	if (my==1) then --红方
		--上
		if ( y-1>= 1 and (not self.mans[map[y-1][x]] or self.mans[map[y-1][x]].my~=my)) then table.insert(d, {x,y-1}) end
		--右
		if ( x+1<= 9 and y<=4  and (not self.mans[map[y][x+1]] or self.mans[map[y][x+1]].my~=my)) then table.insert(d, {x+1,y}) end
		--左
		if ( x-1>= 1 and y<=4 and (not self.mans[map[y][x-1]] or self.mans[map[y][x-1]].my~=my)) then table.insert(d, {x-1,y}) end
	else
		--下
		if ( y+1<= 9  and (not self.mans[map[y+1][x]] or self.mans[map[y+1][x]].my~=my)) then table.insert(d, {x,y+1}) end
		--右
		if ( x+1<= 9 and y>=6  and (not self.mans[map[y][x+1]] or self.mans[map[y][x+1]].my~=my)) then table.insert(d, {x+1,y}) end
		--左
		if ( x-1>= 1 and y>=6 and (not self.mans[map[y][x-1]] or self.mans[map[y][x-1]].my~=my)) then table.insert(d, {x-1,y}) end
	end

	return d;
end

--生成map里面有的棋子
function M:createMans(map)
	map = map or self.map
	for i=1, table.size(map) do
		for n=1, table.size(map[i]) do
			local key = map[i][n];
			--print("key------->", key)
			if key and key ~= "0" then
				self.mans[key] = new("games.chess.man", key);
				self.mans[key].x=n;
				self.mans[key].y=i;
			end
		end
	end
end

function M:shuffle()
	self.map = {
		{'C0','M0','X0','S0','J0','S1','X1','M1','C1'},
		{'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0'},
		{'0' ,'P0','0' ,'0' ,'0' ,'0' ,'0' ,'P1','0' },
		{'Z0','0' ,'Z1','0' ,'Z2','0' ,'Z3','0' ,'Z4'},
		{'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0'},
		{'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0'},
		{'z0','0' ,'z1','0' ,'z2','0' ,'z3','0' ,'z4'},
		{'0' ,'p0','0' ,'0' ,'0' ,'0' ,'0' ,'p1','0' },
		{'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' },
		{'c0','m0','x0','s0','j0','s1','x1','m1','c1'}
	};
	self.mans = {}
	self:createMans()
end

--获得棋子
function M:getClickMan(x, y)
	if (x < 1 or x>9 or y < 1 or y > 10) then return false; end
	return (self.map[y][x] and self.map[y][x]~=nil) and self.map[y][x] or false;
end

--打印棋盘
function M:printMap()
	print("################", "start")
	local tb = {
		c0 = "车",
		m0 = "马",
		x0 = "相",
		s0 = "仕",
		p0 = "炮",
		j0 = "帅",
		c1 = "车",
		m1 = "马",
		x1 = "相",
		s1 = "仕",
		p1 = "炮",
		z0 = "兵",
		z1 = "兵",
		z2 = "兵",
		z3 = "兵",
		z4 = "兵",
--○车○马○炮○士
		C0 = "C0",
		M0 = "M0",
		X0 = "象",
		S0 = "士",
		P0 = "P0",
		J0 = "将",
		C1 = "C1",
		M1 = "M1",
		X1 = "象",
		S1 = "士",
		P1 = "P1",
		Z0 = "卒",
		Z1 = "卒",
		Z2 = "卒",
		Z3 = "卒",
		Z4 = "卒",
	}
	for k, v in pairs(self.map) do
		local str = ""
		for m, n in pairs(v) do
			local ss = self.map[k][m]
			if ss == "0" then
				ss = "00"
			else
				ss = tb[ss]
			end
			str = str .." " ..(ss)
		end
		print(str)
	end
	print("################", "end")
end

--取可走的点
function M:getEnablePoint(man)
	local map = self.map
	local func_name = man.o.bl
	local func = self[func_name]
	local pts = func(self, man.x, man.y, map, man.o.my)
	return pts or {}
end



--
--function M:playByAI(user)
--	local rule = g.rule
--	local pace = g.ai:init(table.concat(user.pace))
--	if not pace then
--		g.desk:showWin(-user.army);
--		print("1111111111111")
--		return
--	end
--	--print("_______playByAI_____1_", tostring(user.pace), tostring(pace))
--	local x1 = tonumber(pace[1])
--	local y1 = tonumber(pace[2])
--	local x2 = tonumber(pace[3])
--	local y2 = tonumber(pace[4])
--	local key = rule.map[y1][x1]
--	self.nowManKey = key;
--	local key = rule.map[y2][x2]
--	if key ~= "0" then
--		self:clickMan(key, x2, y2)
--	else
--		self:clickPoint(x2, y2)
--	end
--	self.isFoul = false --检测是不是长将
--end
--
----看那个着点可以走
--function M:indexOfPs(ps, xy)
--	for i=1, table.size(ps) do
--		if (ps[i][1] == xy[1] and ps[i][2] == xy[2]) then
--			--print("xy", tostring(xy))
--			return true;
--		end
--	end
--	return false;
--end
--
----点击棋子，两种情况，选中或者吃子
--function M:clickMan(key, x, y)
--	local desk = g.desk
--	local man = self.mans[key]
--	local pts = self:getEnablePoint(man)
--
--	--吃子
--	if (self.nowManKey and self.nowManKey ~= key and man.my ~= self.mans[self.nowManKey].my) then
--		--man为被吃掉的棋子
--		if self:indexOfPs(pts, {x,y}) then --ps可下的点
--			man.isShow = false
--			local pace = (rule.mans[self.nowManKey].x -1)  .."" .. (rule.mans[self.nowManKey].y - 1)
--			self.map[self.mans[self.nowManKey].y][self.mans[self.nowManKey].x] = "0"
--			self.map[y][x] = self.nowManKey
--			self.mans[self.nowManKey].x = x
--			self.mans[self.nowManKey].y = y
--			self.mans[self.nowManKey].alpha = 1
--			table.insert(self.pace, pace..(x-1)..(y-1))
--			self.nowManKey = false
--			if (key == "j0") then
--				desk:showWin(-1)
--				return
--			end
--			if (key == "J0") then
--				desk:showWin(1)
--				return
--			end
--			--g.timeout(play.AIPlay,0.5);
--		end
--	else
--		-- 选中棋子
--		if (man.my == self.my) then
--			if rule.mans[self.nowManKey] then
--				rule.mans[self.nowManKey].alpha = 1
--			end
--			man.alpha = 0.8
--			self.nowManKey = key
--			print("______self.nowMankey", self.nowManKey)
--		end
--	end
--end
--
----点击着点
--function M:clickPoint(x, y)
--	local rule = g.rule
--	local key = self.nowManKey
--	local man = rule.mans[key]
--	man:bl()
--	if key then
--		--print("_____________1", tostring(rule.mans[key].ps))
--		if self:indexOfPs(rule.mans[key].ps, {x, y}) then
--			--print("_____________2")
--			local pace = (man.x - 1) .."" .. (man.y - 1)
--			rule.map[man.y][man.x] = "0"
--			rule.map[y][x] = key
--			man.x = x
--			man.y = y
--			man.alpha = 1
--			table.insert(self.pace, pace..(x-1)..(y-1))
--			self.nowManKey = false;
--			--g.setTimeout(play.AIPlay,0.5);
--		else
--			print("不能这么走哦！")
--		end
--	end
--end
--
----检查是否长将
--function M:checkFoul()
--	--local p = self.pace
--	--local len = table.size(p)
--	--if (len>11 and p[len] == p[len-5] and p[len-5] == p[len-9]) then
--	--    local str = p[len-4]
--	--    return string.split(str, "");
--	--end
--	return false;
--end

















return M

